<?php

namespace Casher;

use C\L\AdmController;
use C\R\Payment as PaymentResource;

class PaymentController extends AdmController
{
    public function indexAction()
    {
        $currentPage = $this->request->getQuery('page', 'int', 1);
        $perPage     = $this->request->getQuery('per_page', 'int', 20);

        $payments = $this->s_casher_payment->paginate($currentPage, $perPage);

        return $this->success($payments);
    }

    public function viewAction()
    {
        $id = $this->request->getQuery('id', 'string');

        $payment = $this->s_casher_payment->view($id);

        if (!$payment) {
            return $this->error('查找支付渠道失败');
        }

        $resource = new PaymentResource($payment);

        return $this->success($resource->toArray());
    }

    public function createAction()
    {
        $params = $this->getParams();

        $payment = $this->s_casher_payment->store($params);

        if (!$payment) {
            return $this->error('添加支付渠道失败');
        }

        $resource = new PaymentResource($payment);

        return $this->success($resource->toArray());
    }

    public function updateAction()
    {
        $id = $this->request->getPost('id', 'string');

        $params = $this->getParams();

        $payment = $this->s_casher_payment->newUpdate($id, $params);

        if (!$payment) {
            return $this->error('更新支付渠道失败');
        }

        $resource = new PaymentResource($payment);

        return $this->success($resource->toArray());
    }

    public function deleteAction()
    {
        $id = $this->request->getPost('id', 'string');

        return $this->s_casher_payment->delete($id)
        ? $this->success()
        : $this->error('删除支付渠道失败');
    }

    private function getParams()
    {
        $amountOptions = $this->request->getPost('amount_options') ?? [];

        $otherInfo = json_encode([
        'amount_options' => $amountOptions
        ]);

        return [
        'name'              => $this->request->getPost('name', 'string'),

        'is_enabled_alipay' => $this->request->getPost('is_enabled_alipay', 'string'),
        'alipay_qrcode'     => $this->request->getPost('alipay_qrcode', 'string'),
        'alipay_note'       => $this->request->getPost('alipay_note', 'string'),

        'is_enabled_wechat' => $this->request->getPost('is_enabled_wechat', 'string'),
        'wechat_qrcode'     => $this->request->getPost('wechat_qrcode', 'string'),
        'wechat_note'       => $this->request->getPost('wechat_note', 'string'),

        'is_enabled_bank'   => $this->request->getPost('is_enabled_bank', 'string'),
        'bank_name'         => $this->request->getPost('bank_name', 'string'),
        'bank_note'         => $this->request->getPost('bank_note', 'string'),
        'bank_card_name'    => $this->request->getPost('bank_card_name', 'string'),
        'bank_card_no'      => $this->request->getPost('bank_card_no', 'string'),

        'other_info'        => $otherInfo,
        ];
    }
}
